package com.example.demo.singleton;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public enum  Singleton_3 {
    INSTANCE;
    Singleton_3(){

    }
    public void printHello(){
        System.out.println("Hello");
    }

    public static void main(String[] args) throws InterruptedException {

        //Integer loopNum = 10000000;
        Integer loopNum = 100000;
        Set<Object> instanceSet = new HashSet<>();

        ExecutorService executorService = Executors.newCachedThreadPool();
        CountDownLatch countDownLatch = new CountDownLatch(loopNum);

        for (int i = 0; i < loopNum ; i++) {
            executorService.execute(()->{
                Singleton_3 instance = Singleton_3.INSTANCE;
                instance.printHello();
                instanceSet.add(instance);
                countDownLatch.countDown();
            });
        }

        countDownLatch.await();
        executorService.shutdown();
        System.out.println(instanceSet.size());
    }
}
